package com.esd.maincontrollerabs;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.servlet.ModelAndView;

import com.esd.listener.SimpleConectionListener;
import com.esd.maincontroller.CsairFunction;
import com.gargoylesoftware.htmlunit.AlertHandler;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ConfirmHandler;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;

public class CsairFunctionAbs  implements CsairFunction {
	
	WebClient webClient=null;
	
	private HtmlPage indexPage=null;
	
	private HtmlPage loginPage = null;
	
	private HtmlPage resgisterPage=null;
	
	private HtmlPage findPage=null;
	
	private HtmlPage mobileQueryPage=null;
	
	private HtmlPage specialPage=null;
	
	private HtmlPage specialPassengerPage=null;
	
	private HtmlPage movementPage=null;
	
	private HtmlPage hearingPage=null;
	
	private HtmlPage mzhyLoginPage=null;
	
	private HtmlPage mzhyPage = null;
	
	private HtmlPage mzhyQueryPage=null;
	
	private HtmlPage informationPage=null;
	
	private HtmlPage editPasswordPage=null;
	
	private HtmlPage queryPage=null;
	 
	private HtmlPage passengerPage=null;
	
	private HtmlPage queryOrderPage=null;
	
	private HtmlPage payPage=null;
	
	private HtmlPage resultPage=null;
	
	private Map<String, Object> map=new HashMap<String, Object>();
	
	private boolean loadover=false;
	
	private int count=100;
	
	public CsairFunctionAbs() {
		webClient = new WebClient(BrowserVersion.CHROME);
		new SimpleConectionListener(webClient);
		webClient.setConfirmHandler(new ConfirmHandler() {
			public boolean handleConfirm(Page page, String message) {
				return true;
			}
		});
		webClient.setAlertHandler(new AlertHandler() {
			public void handleAlert(Page page, String message) {
			}
		});
		webClient.getOptions().setThrowExceptionOnScriptError(false);// 当JS执行出错的时候是否抛出异常
		webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);// 当HTTP的状态非200时是否抛出异常
		webClient.getOptions().setCssEnabled(false);// 是否启用CSS
		webClient.getOptions().setJavaScriptEnabled(true); // 很重要，启用JS
		webClient.setAjaxController(new NicelyResynchronizingAjaxController());// 设置支持AJAX
	}
	public WebClient getWebClient(){
		return webClient;
	}
	public void setWebClient(WebClient webClient){ 
		this.webClient=webClient;
	}
	
	
	/**
	 * 加载登录页面
	 */
	@Override
	public Map<String, Object> loadlogin() {
		
		// TODO Auto-generated method stub
		for (int i = 0; i <= count; i++) {
			if (loginPage == null) {
				try {
					loginPage = webClient
							.getPage("http://b2c.csair.com/B2C40/modules/bookingnew/manage/login.html");
				} catch (FailingHttpStatusCodeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (loginPage!= null) {
					break;
				} 
			} 
		}
		for (int i = 0; i <= count; i++) {
			if (loginPage.getElementById("userId") != null) {
				System.out.println("---------登录页面加载完毕--------");
				loadover=true;
				break;
			}
			synchronized (loginPage) {
				try {
					loginPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("--------登录页面正在加载ing-------");
			}
		}
	//	map.put("information", "加载完成");
	//	map.put("loginPage", loginPage);
		return map;
	}
	
	/**
	 * 登录方法
	 */
	@Override
	public Map<String, Object> loginaction(String username, String password) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i <=count; i++) {
			System.out.println("loadover------->"+loadover);
				if(loadover==true){
			
				HtmlInput userId = (HtmlInput) loginPage.getElementById("userId");
		//		userId.focus();
				userId.setValueAttribute(username);
		//		userId.blur();
		//		System.out.println(userId + "--.");
				HtmlPasswordInput passWord = (HtmlPasswordInput) loginPage
						.getElementById("passWord");
		//		passWord.focus();
				passWord.setValueAttribute(password);
		//		passWord.blur();
		//		System.out.println(passWord + "--.");
				try {
					loginPage.getBody()
							.getElementsByAttribute("button", "otitle",
									"预订流程-航班选择-会员登录").get(0).click();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			break;
			}
			synchronized (loginPage) {
				try {
					loginPage.wait(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("正在加载登录页......");
			}
		}
		for (int i = 0; i <=count; i++) {
			if(SimpleConectionListener.loginFlag==true){
				break;
			}
			synchronized (loginPage) {
				try {
					loginPage.wait(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("-----登录中ING-----------");
			}
		}
		
		System.out.println(SimpleConectionListener.loginFlag);
		if(SimpleConectionListener.loginFlag==false){
			int length = loginPage.getBody().getElementsByAttribute("div", "class", "lg-msg zh error").size();
			System.out.println(length);
			if(length > 0){
				String ls_msg= loginPage.getBody().getElementsByAttribute("div", "class", "lg-msg zh error").get(0).asText().toString().trim();
				System.out.println(ls_msg);
				map.put("msg", ls_msg);
				map.put("notice", false);
			}else{
				map.put("msg", "登录失败");
				map.put("notice",false);
			}
		}else{
			map.put("notice",true);
			map.put("msg","登录成功");
			
		}
		// System.out.println(ls_msg);
		loadover=false;
		return map;
	}
	
	/**
	 * 加载注册页
	 */
	@Override
	public Map<String, Object> loadregister() {
		// TODO Auto-generated method stub
		for (int i = 0; i <=count; i++) {
			if(resgisterPage==null){
				try {
					resgisterPage = webClient.getPage("http://b2c.csair.com/B2C40/modules/bookingnew/manage/login.html");
				} catch (FailingHttpStatusCodeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(resgisterPage!=null){
					break;
				}
			}
		}
		 
		for (int i = 0; i <=count; i++) {
			if(resgisterPage.getElementById("userIdReg")!=null){
				System.out.println("-------注册页面加载完毕---------");
				loadover=true;
				break;
			}
			synchronized (resgisterPage) {
				try {
					resgisterPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("-------注册页面正在加载ing-------");
			}
		}
		map.put("information", "加载成功");
		return map;
	}
	
	/**
	 * 注册页发送验证码
	 */
	@Override
	public Map<String, Object> registerYzm(String username) {
		for (int i = 0; i <=count; i++) {
			if(loadover==true){
				HtmlInput userIdReg=(HtmlInput) resgisterPage.getElementById("userIdReg");
				userIdReg.focus();
				userIdReg.setValueAttribute(username);
				userIdReg.blur();
				try {
					resgisterPage.getElementById("getVcode").click();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} //点击获取验证码
				break;
			}else{
				map.put("msg", "您的操作过快!");
				return map;
			}
		}
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String txt = resgisterPage.getBody().getElementsByAttribute("div", "class", "help-txt").get(4).asText();
		if(!txt.isEmpty()){
			map.put("msg", txt);
			return map;
		}
		map.put("msg", "验证码已发送");
		return map;
	}
	
	/**
	 * 注册方法,执行后跳转至登录页面
	 */
	@Override
	public void register(String username,String password,String passwordagain, String vcodeReg) {
		// TODO Auto-generated method stub
		HtmlInput userIdReg=(HtmlInput) resgisterPage.getElementById("userIdReg");
		userIdReg.focus();
		userIdReg.setValueAttribute(username);
		userIdReg.blur();
		
		HtmlInput passwordReg=(HtmlInput) resgisterPage.getElementById("passwordReg");
		passwordReg.focus();
		passwordReg.setValueAttribute(password);
		passwordReg.blur();
		
		HtmlInput passwordRegAgain=(HtmlInput) resgisterPage.getElementById("passwordRegAgain");
		passwordRegAgain.focus();
		passwordRegAgain.setValueAttribute(passwordagain);
		passwordRegAgain.blur();
		
		HtmlInput vcodeRegInput=(HtmlInput) resgisterPage.getElementById("vcodeReg");
		vcodeRegInput.focus();
		vcodeRegInput.setValueAttribute(vcodeReg);
		vcodeRegInput.blur();
		
		try {
			//勾选阅读协议
			resgisterPage.getBody().getElementsByAttribute("i", "class", "check-box").get(0).click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HtmlElement registerButton=resgisterPage.getBody().getElementsByAttribute("button", "otitle", "预订流程-航班选择-会员注册").get(0);
		
		try {
			registerButton.click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		loadover=false;
	}
	
	/**
	 * 加载找回密码页面
	 */
	@Override
	public Map<String, Object> loginfindPassword() {
		// TODO Auto-generated method stub
		for (int i = 0; i <=count; i++) {
			if(findPage==null){
				try {
					findPage=webClient.getPage("http://b2c.csair.com/B2C40/modules/bookingnew/manage/login.html");
				} catch (FailingHttpStatusCodeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(findPage!=null){
					break;
				}
			}
		}
		for (int i = 0; i <= count; i++) {
			if(findPage.getElementById("userIdBus")!=null){
				System.out.println("------找回密码页面加载成功------");
				loadover=true;
				break;
			}
			synchronized (findPage) {
				try {
					findPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("-----找回密码页面正在加载ing-----");
			}
		}
		map.put("information", "加载成功");
		return map;
	}
	
	/**
	 * 找回密码
	 * 前置找回密码页面加载
	 */
	@Override
	public Map<String, Object> getPassword(String username) {
		for (int i = 0; i <=count; i++) {
			if(loadover==true){
				HtmlInput userIdBus=(HtmlInput) findPage.getElementById("userIdBus");
				userIdBus.focus();
				userIdBus.setValueAttribute(username);
				userIdBus.blur();
				try {
					findPage.getElementById("loginpsdBus").click();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//点击获取密码
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}else{
				map.put("msg", "您的操作过快!");
				return map;
			}
		}
		if(findPage.getBody().getElementsByAttribute("font", "color", "red").size()>0){
			map.put("msg", "信息发送成功");
			System.out.println("获取font----->"+ findPage.getBody().getElementsByAttribute("font", "class", "red").get(0).asXml());
			return map;
		}
		map.put("msg", "信息发送失败");
		return map;
		
		
	}
	/**
	 * 找回密码登录
	 */
	@Override
	public Map<String, Object> findpasswordlogin(String username,
			String password) {
		// TODO Auto-generated method stub
		HtmlInput userIdBus=(HtmlInput) findPage.getElementById("userIdBus");
		userIdBus.focus();
		userIdBus.setValueAttribute(username);
		userIdBus.blur();
		HtmlInput passIdBus=(HtmlInput) findPage.getElementById("passwordBusPage");
		passIdBus.focus();
		passIdBus.setValueAttribute(password);
		passIdBus.blur();
		try {
			findPage.getBody().getElementsByAttribute("button", "otitle", "预订流程-航班选择-会员登录").get(1).click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	for (int i = 0; i <=count; i++) {
		if(SimpleConectionListener.loginFlag==true){
			break;
		}
		synchronized (findPage) {
			try {
				findPage.wait(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("-----登录中ING-----------");
		}
	}
	
	System.out.println(SimpleConectionListener.loginFlag);
	if(SimpleConectionListener.loginFlag==false){
		int length = findPage.getBody().getElementsByAttribute("div", "class", "lg-msg zh error").size();
		System.out.println(length);
		if(length > 0){
			String ls_msg= findPage.getBody().getElementsByAttribute("div", "class", "lg-msg zh error").get(0).asText().toString().trim();
			System.out.println(ls_msg);
			map.put("msg", ls_msg);
			map.put("notice", false);
		}else{
			map.put("msg", "登录失败");
			map.put("notice",false);
		}
	}else{
		map.put("notice",true);
		map.put("msg","登录成功");
		
	}
	// System.out.println(ls_msg);
	loadover=false;
		return map;
	}
	
	/**
	 * 加载手机号查询订单页面
	 */
	@Override
	public void loadmobilequery() {
		// TODO Auto-generated method stub
		for (int i = 0; i <=count; i++) {
			if(mobileQueryPage==null){
				try {
					mobileQueryPage=webClient.getPage("http://b2c.csair.com/B2C40/modules/bookingnew/manage/orderLogin.html");
				} catch (FailingHttpStatusCodeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(mobileQueryPage!=null){
					break;
				}
			}
		}
		
		for (int i = 0; i <=count; i++) {
			if(mobileQueryPage.getBody().getElementsByAttribute("input", "name", "phone").size()>0){
				System.out.println("-------手机号查询订单页面加载完毕---------");
				loadover=true;
				break;
			}
			synchronized (mobileQueryPage) {
				try {
					mobileQueryPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("-------手机号查询订单页面正在加载ing-------");
			}
		}
	}
	
	/**
	 * 手机查询订单发送验证码
	 */
	@Override
	public Map<String, Object> mobileQueryYzm(String username) {
		for (int i = 0; i <=count; i++) {
			if(loadover==true){
				HtmlInput usernameInput=(HtmlInput) mobileQueryPage.getBody().getElementsByAttribute("input", "name", "phone").get(0);
				usernameInput.focus();
				usernameInput.setValueAttribute(username);
				usernameInput.blur();
				try {
					mobileQueryPage.getBody().getElementsByAttribute("button", "class", "btn getCode").get(0).click();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} // 点击获取验证码
				break;
			}else{
				map.put("msg", "您的操作过快!");
				return map;
			}
		}
		map.put("msg", "验证码已发送");
		return map;
	}

	/**
	 * 手机号查询订单
	 */
	@Override
	public Map<String , Object> mobilequery(String username, String vcodeReg) {
		// TODO Auto-generated method stub
		HtmlInput usernameInput=(HtmlInput) mobileQueryPage.getBody().getElementsByAttribute("input", "name", "phone").get(0);
		usernameInput.focus();
		usernameInput.setValueAttribute(username);
		usernameInput.blur();
		
		HtmlInput vcodeRegInput=(HtmlInput) mobileQueryPage.getBody().getElementsByAttribute("input", "name", "mcode").get(0);
		usernameInput.focus();
		usernameInput.setValueAttribute(vcodeReg);
		usernameInput.blur();
		
		try {
			mobileQueryPage.getBody().getElementsByAttribute("button", "otilte", "预订流程-航班选择-用手机号查询订单/查询").get(0).click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(SimpleConectionListener.loginFlag==false){
			map.put("msg", "查询失败");
			map.put("notice",false);
		}else{
			map.put("notice",true);
			map.put("msg","查询成功");
		}
		return map;
	}

	/**
	 * 出行帮助页面加载
	 */
	@Override
	public Map<String, Object> specialservice() {
		// TODO Auto-generated method stub
		Map<String, Object> map=new HashMap<String, Object>();
		for (int i = 0; i <=count; i++) {
			try {
				specialPage=webClient.getPage("http://www.csair.com/cn/tourguide/special/special_passengers/index.shtml");
			} catch (FailingHttpStatusCodeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(specialPage.getBody().getElementsByAttribute("div", "class", "second-form-box").size()>0){
				System.out.println("-------页面加载完成---------");
				break;
			}
			synchronized (specialPage) {
				try {
					specialPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("-------页面正在加载ing----------");
			}
		}
		HtmlElement titles = specialPage.getBody().getElementsByAttribute("ul", "class", "check-in-list").get(0);
		 String a1 = titles.getElementsByTagName("li").get(0).asXml();
		 String a2 = titles.getElementsByTagName("li").get(3).asXml();
		 String a3 = titles.getElementsByTagName("li").get(4).asXml();
		 
		 String userId = specialPage.getElementById("userinfo").asText();
			map.put("title1", a1);
			map.put("title2", a2);
			map.put("title3", a3);
			map.put("user", userId);
			return map;
	}

	/**
	 * 特殊旅客出行提示加载页面
	 */
	@Override
	public Map<String, Object> specialpassenger() {
		// TODO Auto-generated method stub
		Map<String, Object> map=new HashMap<String, Object>();
		for (int i = 0; i <=count; i++) {
			try {
				specialPassengerPage=webClient.getPage("http://www.csair.com/cn/tourguide/special/special_passengers/");
			} catch (FailingHttpStatusCodeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(specialPassengerPage.getBody().getElementsByAttribute("div", "class", "form-con-list").size()>0){
				System.out.println("---------页面加载完成------------");
				break;
			}
			synchronized (specialPassengerPage) {
				try {
					specialPassengerPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("-------页面加载中ing------------");
			}
		}
		String doc = specialPassengerPage.getBody().getElementsByAttribute("div", "class", "form-con-list").get(0).asXml();
		String userId = specialPassengerPage.getElementById("userinfo").asText();
		map.put("user", userId);
		map.put("doc", doc);
//		System.out.println(doc);
		return map;
	}

	/**
	 * 行动障碍旅客页面加载
	 */
	@Override
	public Map<String, Object> movement() {
		// TODO Auto-generated method stub
		Map<String, Object> map=new HashMap<String, Object>();
		for (int i = 0; i <=count; i++) {
			try {
				movementPage=webClient.getPage("http://www.csair.com/cn/tourguide/special/movement_disorder/");
			} catch (FailingHttpStatusCodeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(movementPage.getBody().getElementsByAttribute("div", "class", "form-con-list").size()>0){
				System.out.println("-------页面加载完成------");
				break;
			}
			synchronized (movementPage) {
				try {
					movementPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("-----页面加载中ing-------");
			}
		}
		String doc = movementPage.getBody().getElementsByAttribute("div", "class", "form-con-list").get(0).asXml();
		String userId = movementPage.getElementById("userinfo").asText();
		map.put("user", userId);
		map.put("doc", doc);
		return map;
	}

	/**
	 * 视力或听力障碍旅客页面加载
	 */
	@Override
	public Map<String, Object> hearing() {
		// TODO Auto-generated method stub
		Map<String, Object> map= new HashMap<String, Object>();
		for (int i = 0; i <=count; i++) {
			try {
				hearingPage=webClient.getPage("http://www.csair.com/cn/tourguide/special/visual_or_hearing_impairment/");
			} catch (FailingHttpStatusCodeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(hearingPage.getBody().getElementsByAttribute("div", "class", "form-con-list").size()>0){
				System.out.println("------页面加载完成------");
				break;
			}
			synchronized (hearingPage) {
				try {
					hearingPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("------页面加载中ing-------");
			}
		}
		String doc=hearingPage.getBody().getElementsByAttribute("div", "class", "form-con-list").get(0).asXml();
		String userId = hearingPage.getElementById("userinfo").asText();
		map.put("user", userId);
		map.put("doc", doc);
		return map;
	}
	
	/**
	 * 明珠会员登录页面加载
	 */
	@Override
	public Map<String, Object> mzhyloadlogin() {
		// TODO Auto-generated method stub
		for (int i = 0; i <=count; i++) {
			if(mzhyLoginPage==null){
				try {
					mzhyLoginPage=webClient.getPage("http://b2c.csair.com/B2C40/modules/bookingnew/manage/login.html");
				} catch (FailingHttpStatusCodeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(mzhyLoginPage!=null){
					break;
				}
			}
		}
		Map<String, Object> map=new HashMap<String, Object>();
		for (int i = 0; i <=count; i++) {
			if (mzhyLoginPage.getElementById("userId")!=null) {
				System.out.println("---------明珠会员登录页面加载完毕--------");
				loadover=true;
				break;
			}
			synchronized (mzhyLoginPage) {
				try {
					mzhyLoginPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("--------明珠会员登录页面正在加载ing-------");
			}
		}
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		map.put("information", "加载完成");
		return map;
	}

	/**
	 * 明珠会员登录
	 */
	@Override
	public Map<String, Object> mzhylogin(String username, String password) {
		// TODO Auto-generated method stub
		for (int i = 0; i <=count; i++) {
			if(loadover==true){
				HtmlInput usernameInput=(HtmlInput) mzhyLoginPage.getElementById("userId");
				usernameInput.focus();
				usernameInput.setValueAttribute(username);
				usernameInput.blur();
				
				HtmlInput passwordInput=(HtmlInput) mzhyLoginPage.getElementById("passWord");
				passwordInput.focus();
				passwordInput.setValueAttribute(password);
				passwordInput.blur();
				
				try {
					mzhyLoginPage.getBody().getElementsByAttribute("button", "otitle", "预订流程-航班选择-会员登录").get(0).click();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			break;
		}
		
		for (int i = 0; i <=count; i++) {
			if(SimpleConectionListener.loginFlag==true){
				break;
			}
			synchronized (mzhyLoginPage) {
				try {
					mzhyLoginPage.wait(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("-----登录中ING-----------");
			}
		}
		System.out.println(SimpleConectionListener.loginFlag);
		if(SimpleConectionListener.loginFlag==false){
			int length = mzhyLoginPage.getBody().getElementsByAttribute("div", "class", "lg-msg zh error").size();
			System.out.println(length);
			if(length > 0){
				String ls_msg= mzhyLoginPage.getBody().getElementsByAttribute("div", "class", "lg-msg zh error").get(0).asText().toString().trim();
				System.out.println(ls_msg);
				map.put("msg", ls_msg);
				map.put("notice", false);
			}else{
				map.put("msg", "登录失败");
				map.put("notice",false);
			}
		}else{
			map.put("notice",true);
			map.put("msg","登录成功");
		}
		loadover=false;
		return map;
	}

	/**
	 * 明珠会员判断
	 
	@Override
	public void mzhypd() {
		// TODO Auto-generated method stub
		try {
			mzhyPage = webClient.getPage("http://skypearl.csair.com/cn/index.html");
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i = 0; i <=100; i++) {
			if(mzhyPage.getBody().getElementsByAttribute("a", "class", "mlink").size()>0){
				System.out.println("----明珠会员页面加载完成-----");
				break;
			}
			synchronized (mzhyPage) {
				try {
					mzhyPage.wait(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("-----明珠会员页面加载中ing-----");
			}
		}
		try {
			mzhyPage.getBody().getElementsByAttribute("a", "class", "mlink").get(0).click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		if(SimpleConectionListener.resurl3.startsWith("http://skypearl.csair.com/skypearl/cn/memberArea.action?")){
//			System.out.println("jinru!!!!!!!!!!!!!!!");
//			mzhyquery();
//			return;
//		}
//		System.out.println("没进去!");
	
	}*/

	/**
	 * 明珠会员基本信息页面
	 
	@Override
	public ModelAndView mzhyquery() {
		// TODO Auto-generated method stub
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			mzhyQueryPage=webClient.getPage(SimpleConectionListener.resurl3);
			System.out.println(mzhyQueryPage+"++++++++++++++++++++++");
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.println(mzhyQueryPage.asXml());
		if(mzhyQueryPage.getElementById("showPage")==null){
			System.out.println("showPage不存在!");
			return new ModelAndView("redirect:mzhy.action");
		}
		String src = mzhyQueryPage.getElementById("showPage").getAttribute("src");
		System.out.println("src---->"+src);
		try {
			informationPage = webClient.getPage(src);
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (RuntimeException e) {
		// TODO Auto-generated catch block
			System.out.println("连接超时了");
		}
//		 DomElement baseinfoForm = mzhyQueryPage.getElementById("baseinfoForm");
//		System.out.println("baseinfoForm--->"+baseinfoForm);
//		map.put("informationPage", baseinfoForm);
		System.out.println(informationPage.asXml());
		map.put("informationPage", informationPage.asXml());
		return new ModelAndView("mzhyquery",map);
	}*/

	/**
	 * 明珠会员帐号信息保存
	 */
	@Override
	public void basicinformationsave(String b2cName,
			String title, String birthday, String schoolLevel,
			String professionCode, String professionTitle, String officeName) {
		// TODO Auto-generated method stub
		
		informationPage.getElementById("loadimg").remove();
		
		HtmlInput b2cNameInput=(HtmlInput) informationPage.getElementById("b2cName");
		b2cNameInput.focus();
		b2cNameInput.setValueAttribute(b2cName);
		b2cNameInput.blur();
		
		HtmlSelect titleSelect=(HtmlSelect) informationPage.getElementById("title");
		titleSelect.focus();
		titleSelect.setSelectedAttribute(title, true);
		titleSelect.blur();
		
		HtmlInput birthdayInput=(HtmlInput) informationPage.getElementById("svcPcAllinfo.svcPcBasicInfoDto.birthday");
		birthdayInput.focus();
		birthdayInput.setValueAttribute(birthday);
		birthdayInput.blur();
		
		HtmlSelect schoolLevelSelect=(HtmlSelect) informationPage.getElementById("schoolLevel");
		schoolLevelSelect.focus();
		schoolLevelSelect.setSelectedAttribute(schoolLevel, true);
		schoolLevelSelect.blur();
		
		HtmlSelect professionCodeSelect=(HtmlSelect) informationPage.getElementById("professionCode");
		professionCodeSelect.focus();
		professionCodeSelect.setSelectedAttribute(professionCode, true);
		professionCodeSelect.blur();
		
		HtmlInput professionTitleInput=(HtmlInput) informationPage.getElementById("svcPcAllinfo.svcPcBasicInfoDto.professionTitle");
		professionTitleInput.focus();
		professionTitleInput.setValueAttribute(professionTitle);
		professionTitleInput.blur();
		
		HtmlInput officeNameInput=(HtmlInput) informationPage.getElementById("svcPcAllinfo.svcPcBasicInfoDto.officeName");
		officeNameInput.focus();
		officeNameInput.setValueAttribute(officeName);
		officeNameInput.blur();
		
		try {
			informationPage.getElementById("savebutton").click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	//	System.out.println(SimpleConectionListener.resurl+"------->"+SimpleConectionListener.loginFlag+"!!!!!!!!!");
		
	}

	/**
	 * 修改密码页面加载
	 */
	@Override
	public Map<String, Object> getEditPassword() {
		// TODO Auto-generated method stub
		for (int i = 0; i <=count; i++) {
			if(editPasswordPage==null){
				try {
					editPasswordPage=webClient.getPage("http://b2c.csair.com/B2C40/modules/bookingnew/manage/forgetpsw.html");
				} catch (FailingHttpStatusCodeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(editPasswordPage!=null){
					break;
				}
			}
		}
		
		for (int i = 0; i <=count; i++) {
			if(editPasswordPage.getBody().getElementsByAttribute("input", "class", "forgetinput").size()>0){
				System.out.println("------修改密码页面加载完毕------");
				loadover=true;
				break;
			}
			synchronized (editPasswordPage) {
				try {
					editPasswordPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("------修改密码页面正在加载ing--------");
			}
		}
		map.put("information", "加载成功");
		return map;
	}

	/**
	 * 修改密码
	 */
	@Override
	public Map<String, Object> changePassword(String oldpassword,
			String newpassword, String newpasswordagain) {
		// TODO Auto-generated method stub
		Map<String, Object> map=new HashMap<String, Object>();
		System.out.println("我进来了!!!=============");
		for (int i = 0; i <=count; i++) {
			if(loadover==true){
				HtmlPasswordInput oldPassword=(HtmlPasswordInput) editPasswordPage.getBody().getElementsByAttribute("input", "data-index", "0").get(0);
				oldPassword.focus();
				oldPassword.setValueAttribute(oldpassword);
				oldPassword.blur();
				
				HtmlPasswordInput newPassword=(HtmlPasswordInput) editPasswordPage.getBody().getElementsByAttribute("input", "data-index", "1").get(0);		
				newPassword.focus();
				newPassword.setValueAttribute(newpassword);
				newPassword.blur();
				
				HtmlPasswordInput newPasswordAgain=(HtmlPasswordInput) editPasswordPage.getBody().getElementsByAttribute("input", "data-index", "2").get(0);
				newPasswordAgain.focus();
				newPasswordAgain.setValueAttribute(newpasswordagain);
				newPasswordAgain.blur();
				try {
					editPasswordPage.getBody().getElementsByAttribute("a", "class", "changepswinfo").get(0).click();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
		}
		DomElement box = editPasswordPage.getElementById("alert_msg_box");
//		System.out.println("box-------->"+box);
		if(box.asXml().length()>0){
			map.put("msg", "您已成功修改密码，请保管好密码。现在你需重新登录校验。");
		}
		loadover=false;
		return map;
	}

	/**
	 * 加载登录后查询页面
	 
	@Override
	public Map<String, Object> loadquery() {
		// TODO Auto-generated method stub
		HtmlPage htmlPage = null;
		System.out.println("SimpleConectionListener.resurl1---->"+SimpleConectionListener.resurl1);
		for (int i = 0; i <= 100; i++) {
			try {
				htmlPage = webClient
						.getPage(SimpleConectionListener.resurl1);
			} catch (FailingHttpStatusCodeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (htmlPage != null) {
				break;
			}
		}

		for (int i = 0; i <= 100; i++) {
			if (htmlPage.getElementById("userinfo") != null) {
				System.out
						.println("----------查询订单页面加载完毕------------------");
				break;
			}
			synchronized (htmlPage) {
				try {
					htmlPage.wait(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out
						.println("----------查询订单页面正在加载ing------------------");
			}
		}

		String userId = htmlPage.getElementById("userinfo").asText();
		map.put("user", userId);
		return map;
	}*/
	
	/**
	 * 航班查询
	 */
	@Override
	public Map<String, Object> query(String c1, String c2, String date,
			int number1, int number2, int number3) {
		// TODO Auto-generated method stub
		String queryUrl = "http://b2c.csair.com/B2C40/modules/bookingnew/main/flightSelectDirect.html?t=S&c1="
				+ c1
				+ "&c2="
				+ c2
				+ "&d1="
				+ date
				+ "&at="+number1+"&ct="+number2+"&it="+number3;

		try {
			queryPage = webClient.getPage(queryUrl);
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i <= 100; i++) {
			if (queryPage.getBody()
					.getElementsByAttribute("div", "class", "sp-trip-body")
					.size() > 0) {
				System.out
						.println("---------------查询页面加载完毕------------------------------");
				break;
			}else if(queryPage.getBody()
					.getElementsByAttribute("div", "class", "alert-info alert-error").size()>0){
				System.out.println("没有该航班!!!!");
				String alert = queryPage.getBody().getElementsByAttribute("div", "class", "alert-info alert-error").get(0).asText();
				map.put("alert", alert);
				return map;
			}
			synchronized (queryPage) {
				try {
					queryPage.wait(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out
						.println("---------------查询页面正在加载ing------------------------");
			}
		}
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sp_trip_body = queryPage.getBody()
				.getElementsByAttribute("div", "class", "sp-trip-body")
				.get(0).asXml();
		
		Document doc = Jsoup.parse(sp_trip_body);
		Elements uls = doc.select("ul");
		int i = 0;
		for (Element ul : uls) {
			Elements sides = ul.select("div.side");
			for (Element side : sides) {
				String time = side.select("div.sp-trip-time").get(0).text();
				String hour = time.subSequence(0, 2).toString();
				String min = time.substring(3, 5);
				side.select("div.sp-trip-time").get(0)
						.text(hour + "点" + min + "分");
			}
			Elements divs = ul.select("li.cabin-0 div.price");
			String time1 = ul.select("div.sp-trip-time").get(0).text();
			String city1 = ul.select("div.sp-trip-city").get(0).text();

			String time2 = ul.select("div.sp-trip-time").get(1).text();
			String city2 = ul.select("div.sp-trip-city").get(1).text();

			String version = ul.select("div.sp-trip-duration").text();
			String stop = ul.select("div.sp-trip-stops").text();

			for (Element div : divs) {
				Elements spans = div.select(".benefits ");
				for(Element span:spans){
					span.remove();
				}
				String price2 = div.text().substring(1).trim()+"元";
				String price = time1 + " " + city1 + " 航班号:" + stop + " "
						+ version + " " + time2 + " " + city2 + " "
						+ div.text().substring(1).trim() + "元";
				div.empty();
				div.append("" + price2 + ""
						+ "<input type=\"radio\" name=\"radio\" value=\"" + price + "\" title=\"" + price
						+ "\" />");
			}
			i++;

		}

		// doc.select("li.sp-trip-intro").remove();
		String userId = queryPage.getElementById("userinfo").asText();
		System.out.println("userId--->"+userId);
		String person = queryPage.getBody()
				.getElementsByAttribute("div", "class", "flightTdata")
				.get(0).asText();
		map.put("result", doc);
		map.put("user", userId);
		map.put("person", person);
		return map;
	}


	/**
	 * 无航班
	
	@Override
	public Map<String, Object> noflight() {
		// TODO Auto-generated method stub
		System.out.println("进入无航班");
		Map<String , Object> map = new HashMap<String, Object>();
		String alert = queryPage.getBody().getElementsByAttribute("div", "class", "alert-info alert-error").get(0).asText();
		map.put("alert", alert);
		return map;
	} */
	
	/**
	 * 航班列表
	 */
	@Override
	public String getLi(String href) {
		// TODO Auto-generated method stub
		System.out.println("方法中的href---->"+href);
		HtmlElement li = queryPage.getBody()
				.getElementsByAttribute("li", "href", href).get(0);
		try {
			li.click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String listLi = li.getParentNode().getLastChild().asXml();
		Document lis = Jsoup.parse(listLi);
		
		Elements divList = lis.select("div.cabin-detail-list");
		 for (Iterator<Element> iterator = divList.iterator(); iterator.hasNext();) {
			Element div = (Element) iterator.next();
			
			String deName = div.select("div.ca-de-name").text();
			String price = div.select("div.ca-de-price").text();
			String leftNum = div.select("span.leftNum.show").text();
			// div.select("a").html(deName + " "+ price + " " + leftNum + "预
			// 订");
			div.select("a").attr("title",
					deName + " " + price + " " + leftNum + " 预  订");
		}
//		for (Element div : divList) {
//			
//			String deName = div.select("div.ca-de-name").text();
//			String price = div.select("div.ca-de-price").text();
//			String leftNum = div.select("span.leftNum.show").text();
//			// div.select("a").html(deName + " "+ price + " " + leftNum + "预
//			// 订");
//			div.select("a").attr("title",
//					deName + " " + price + " " + leftNum + " 预  订");
//		}
		
//		System.out.println("lis.toString()---->"+lis.toString());
		return  lis.toString();
	}
	
	/**
	 * 点击订票
	 */
	@Override
	public void getBuyButton(String href) {
		// TODO Auto-generated method stub
		href = "#" + href;
		HtmlElement buyButton = queryPage.getBody()
				.getElementsByAttribute("a", "href", href).get(0);
		try {
			buyButton.click();
			System.out.println("点击订票");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 不登录直接购票
	 */
	@Override
	public void nologin() {
		// TODO Auto-generated method stub
		HtmlElement noLoginbtnInput=(HtmlElement) queryPage.getElementById("noLogin-btn");
		try {
			noLoginbtnInput.click();
			System.out.println("点击不登录直接购票");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 非登录用户订票登录
	 */
	@Override
	public void tologin(String username, String password) {
		// TODO Auto-generated method stub
		
		HtmlInput userIdInput=(HtmlInput) queryPage.getElementById("userId");
		userIdInput.focus();
		userIdInput.setValueAttribute(username);
		userIdInput.blur();
		
		HtmlInput passWordInput=(HtmlInput) queryPage.getBody().getElementsByAttribute("input", "name", "passWord").get(0);
		passWordInput.focus();
		passWordInput.setValueAttribute(password);
		passWordInput.blur();
		
		try {
			queryPage.getBody().getElementsByAttribute("button", "otitle", "预订流程-航班选择-会员登录").get(0).click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 非登录用户订票忘记密码
	 
	@Override
	public void forgetpassword(String username) {
		// TODO Auto-generated method stub
		HtmlInput userIdInput=(HtmlInput) queryPage.getElementById("userId");
		userIdInput.focus();
		userIdInput.setValueAttribute(username);
		userIdInput.blur();
		
		try {
			queryPage.getElementById("loginpsd").click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/

	/**
	 * 非登录用户订票注册发送验证码
	 
	@Override
	public void toRegisterYzm(String username) {
		// TODO Auto-generated method stub
		HtmlInput userIdInput=(HtmlInput) queryPage.getElementById("userId");
		userIdInput.focus();
		userIdInput.setValueAttribute(username);
		userIdInput.blur();
		try {
			queryPage.getElementById("getVcode").click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/

	/**
	 * 非登录用户订票注册
	 
	@Override
	public void toRegister(String username, String vcodeReg, String password,
			String passwordagin) {
		// TODO Auto-generated method stub
		HtmlInput userIdInput=(HtmlInput) queryPage.getElementById("userId");
		userIdInput.focus();
		userIdInput.setValueAttribute(username);
		userIdInput.blur();
		
		HtmlInput vcodeRegInput=(HtmlInput) queryPage.getElementById("vcodeReg");
		vcodeRegInput.focus();
		vcodeRegInput.setValueAttribute(vcodeReg);
		vcodeRegInput.blur();
		
		HtmlInput passwordRegInput=(HtmlInput) queryPage.getElementById("passwordReg");
		passwordRegInput.focus();
		passwordRegInput.setValueAttribute(password);
		passwordRegInput.blur();
		
		HtmlInput passwordRegAgainInput=(HtmlInput) queryPage.getElementById("passwordRegAgain");
		passwordRegAgainInput.focus();
		passwordRegAgainInput.setValueAttribute(passwordagin);
		passwordRegAgainInput.blur();
		
		try {
			queryPage.getBody().getElementsByAttribute("i", "class", "check-box").get(0).click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			queryPage.getBody().getElementsByAttribute("button", "otitle", "预订流程-航班选择-会员注册").get(0).click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
	
	/**
	 * 加载乘客信息页面
	 */
	@Override
	public Map<String, Object> toaddpassenger() {
		// TODO Auto-generated method stub
		for (int i = 0; i <= count; i++) {
			if (SimpleConectionListener.resurl.length() > 0) {
				break;
			} else {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("----------链接正在加载ing------------------");
			}
		}
		try {
			passengerPage = webClient.getPage(SimpleConectionListener.resurl);
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i = 0; i <= 100; i++) {
			if (!"总价 ¥0".equals(passengerPage.getElementById("price-total")
					.asText())) {
				System.out.println("----------添加乘客人信息页面加载完毕------------------");
				break;
			}
			synchronized (passengerPage) {
				try {
					passengerPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out
						.println("----------添加乘客人信息页面正在加载ing------------------");
			}
		}
		String userId = passengerPage.getElementById("userinfo").asText();
		System.out.println("userId----->"+userId);
		map.put("user", userId);
		map.put("passengerInfo", passengerPage.getBody()
				.getElementsByAttribute("div", "class", "flight-summary-box")
				.get(0).asXml());
		System.out
				.println(passengerPage.getElementById("price-total").asText());
		return map;
	}
	/**
	 * 填加乘客信息
	 */
	@Override
	public void addPassenger(String psgName, String pIdCard, String contact_name,
			String contact_moblie, String e_mail) {
		// TODO Auto-generated method stub
		// 乘机人姓名
		//System.out.println(psgName+"=="+pIdCard+"=="+contact_name+"=="+contact_moblie+"=="+e_mail);
		HtmlInput psgNameInput = passengerPage.getElementByName("psgName");
		psgNameInput.focus();
		psgNameInput.setValueAttribute(psgName);
		psgNameInput.blur();
		System.out.println(psgNameInput+"=========");
		// 身份证号
		HtmlInput pIdCardInput = passengerPage.getElementByName("pIdCard");
		pIdCardInput.focus();
		pIdCardInput.setValueAttribute(pIdCard);
		pIdCardInput.blur();
		System.out.println(pIdCardInput+"========");
		// 联系人姓名
		HtmlInput contact_nameInput = (HtmlInput) passengerPage
				.getElementById("contact_name");
		contact_nameInput.focus();
		contact_nameInput.setValueAttribute(contact_name);
		contact_nameInput.blur();
		System.out.println(contact_nameInput+"========");
		// 联系人手机号
		HtmlInput contact_moblieInput = (HtmlInput) passengerPage
				.getElementById("contact-moblie");
		contact_moblieInput.focus();
		contact_moblieInput.setValueAttribute(contact_moblie);
		contact_moblieInput.blur();
		System.out.println(contact_moblieInput+"==========");
		//E_mail
		HtmlInput emailInput=(HtmlInput) passengerPage.getElementById("email");
		emailInput.focus();
		emailInput.setValueAttribute(e_mail);
		emailInput.blur();
		System.out.println(emailInput+"========");
		
		//passengerPage.getElementById("protocol-checkbox").click();//阅读条款
		passengerPage.getElementById("protocol-checkbox").setAttribute("class", "protocol-checkbox checked");
		
		try {
			HtmlPage a=passengerPage.getElementById("submitForm").click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}// 提交订单
		//System.out.println(a.asXml());
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 加载订单查询页面
	 */
	@Override
	public Map<String, Object> queryOrder() {
		// TODO Auto-generated method stub
		for (int i = 0; i <= 100; i++) {
			try {
				queryOrderPage = webClient.getPage(SimpleConectionListener.resurl1);
//				queryOrderPage = webClient.getPage("http://b2c.csair.com/B2C40/modules/ordernew/orderManagementFrame.jsp");
				System.out.println("queryOrderPage------->"+queryOrderPage);
			} catch (FailingHttpStatusCodeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				Thread.sleep(4000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (queryOrderPage != null) {
				break;
			}
		}

		for (int i = 0; i <= 100; i++) {
			if (queryOrderPage.getBody().getElementsByAttribute("a", "class", "view-order").size() > 0) {
				System.out.println("----------查询订单页面加载完毕------------------");
				break;
			}else if(queryOrderPage.getBody()
					.getElementsByAttribute("span", "class", "red").size()>0){
					noorder();
			}
			synchronized (queryOrderPage) {
				try {
					queryOrderPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("----------查询订单页面正在加载ing------------------");
			}
		}
		String order = queryOrderPage.getBody().getElementsByAttribute("div", "class", "psg_content").get(0).asXml();
		String userId = queryOrderPage.getElementById("userinfo").asText();
		map.put("order", order);
		map.put("userId", userId);
		return map;
	}

	/**
	 * 无订单
	 */
	@Override
	public ModelAndView noorder() {
		// TODO Auto-generated method stub
		String alert = queryOrderPage.getBody().getElementsByAttribute("span", "class", "red").get(0).asText();
		map.put("orderalert", alert);
		return new ModelAndView("query", map);
	}

	/**
	 * 支付页面加载
	 */
	@Override
	public Map<String, Object> goPayPage(String orderNo) {
		// TODO Auto-generated method stub
		try {
			payPage = webClient
					.getPage("http://b2c.csair.com/B2C40/uppPayment/jaxb/payment.ao?orderNo="
							+ orderNo + "&inter=1");
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for (int i = 0; i <= 100; i++) {
			if (payPage.getBody()
					.getElementsByAttribute("div", "class", "order-summary")
					.get(0) != null) {
				System.out.println("----------支付页面加载完毕------------------");
				break;
			}
			synchronized (payPage) {
				try {
					payPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("----------支付页面正在加载ing------------------");
			}
		}
		String order_summary = payPage.getBody()
				.getElementsByAttribute("div", "class", "order-summary").get(0)
				.asXml();
		String userId = queryPage.getElementById("userinfo").asText();
		map.put("user", userId);
		map.put("orderInfo", order_summary);
		return map;
	}
	
	/**
	 * 支付页获取验证码
	 */
	@Override
	public Map<String, Object> yzmClick(String accountNo, String buyerName,
			String idCard, String mobile) {
		// TODO Auto-generated method stub
		HtmlRadioButtonInput borrowCard = (HtmlRadioButtonInput) payPage
				.getElementById("borrowCard"); // 借记卡单选框
		borrowCard.setChecked(true);
		try {
			borrowCard.click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		HtmlInput accountNoInput = (HtmlInput) payPage
				.getElementById("ACCOUNTNO"); // 卡号输入框
		accountNoInput.focus();
		accountNoInput.setValueAttribute(accountNo);
		accountNoInput.blur();

		HtmlInput buyerNameInput = (HtmlInput) payPage
				.getElementById("BUYERNAME"); // 持卡人姓名
		buyerNameInput.focus();
		buyerNameInput.setValueAttribute(buyerName);
		buyerNameInput.blur();

		HtmlInput idCardInput = (HtmlInput) payPage.getElementById("IDCARD"); // 持卡人证件号
		idCardInput.focus();
		idCardInput.setValueAttribute(idCard);
		idCardInput.blur();

		HtmlInput mobileInput = (HtmlInput) payPage.getElementById("MOBILE"); // 银行预留手机号
		mobileInput.focus();
		mobileInput.setValueAttribute(mobile);
		mobileInput.blur();
		try {
			payPage.getElementById("buttonForCode").click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 点击获取验证码按钮

		for (int i = 0; i <= 100; i++) {
			if (!"".equals(payPage.getElementById("phoneTips").asText().trim())) {
				System.out.println("----------加载验证信息------------------");
				break;
			}
			synchronized (payPage) {
				try {
					payPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(payPage.asXml());
				System.out.println("----------加载验证信息ing------------------");
			}
		}
		String codeMsg = payPage.getElementById("phoneTips").asText().trim();
		map.put("msg", codeMsg);
		return map;
	}

	/**
	 * 执行支付
	 */
	@Override
	public Map<String, Object> pay(String codeName) {
		// TODO Auto-generated method stub
		HtmlInput codeNameInput = (HtmlInput) payPage
				.getElementById("CODENAME"); // 验证码
		codeNameInput.focus();
		codeNameInput.setValueAttribute(codeName);
		codeNameInput.blur();
		HtmlInput payButton = payPage.getElementByName("upp_pay_btn"); // 执行支付按钮
		try {
			resultPage = payButton.click();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i = 0; i <= 100; i++) {
			if (!resultPage.getBody()
					.getElementsByAttribute("div", "class", "cubox").get(0)
					.asText().trim().startsWith("支付失败")) {
				System.out.println("----------csresult页面加载完毕------------------");
				break;
			}
			synchronized (resultPage) {
				try {
					resultPage.wait(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out
						.println("----------csresult页面正在加载ing------------------");
			}
		}

		map.put("result",
				resultPage.getBody()
						.getElementsByAttribute("div", "class", "cubox").get(0)
						.asText());
		return map;
	}
	
	

	

	

	

	

	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
}


